import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.plaf.SliderUI;
import javax.swing.text.PlainDocument;

public class Client {
	
	@SuppressWarnings("deprecation")
	public static void main(String[] pArgs) {
		if(pArgs.length<3){
			System.out.println("usage: java Client host port boardnum");
			return;
		}
		
		try{
			Socket lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
			PrintWriter lOut=new PrintWriter(lSocket.getOutputStream());
			BufferedReader lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
	
            lOut.println(pArgs[2]);
            lOut.flush();

            String lLine=lIn.readLine();
            
            
            //create a list of the string
            ArrayList<String> level = new ArrayList<String>();
            
            long start = new Date().getTime();
            
            
            //read number of rows
            int lNumRows=Integer.parseInt(lLine);

            //read each row
            for(int i=0;i<lNumRows;i++)
            {
                lLine=lIn.readLine();
                level.add(lLine);
            }
            
            //read number of columns
            int lNumColumns=level.get(0).length();
            
            //build the board 
            Board board = new Board (lNumRows,lNumColumns, level);
            State.setBoard(board);
            // create playerNBoxes positions
            PlayerNBoxes playerNBoxes = new PlayerNBoxes(lNumRows,lNumColumns, level);
            
            PlayerNBoxesReverse playerNBoxesR = new PlayerNBoxesReverse(lNumRows,lNumColumns, level);
            
//            System.out.println(playerNBoxes);
            byte[][] jb = Helper.joinBoards(board, playerNBoxes);
            Helper.printjoint(jb);
//            System.out.println(board);
            String solution = "";
            
            ReverseSolverStarter reverse = new ReverseSolverStarter(board, playerNBoxesR);
            ForwardSolverStarter forward = new ForwardSolverStarter(board, playerNBoxes);
            reverse.start();
            forward.start();
            
            while ( solution == "" && ((new Date().getTime()) - start ) < 58000 ) {
            	if ( !forward.isAlive()){
        			if ( forward.getSolution() != ""){
            			solution = forward.getSolution();
        			}
            	}
            	 Thread.sleep(10);
            }
            if ( solution == ""){
	        	if ( !reverse.isAlive()){
	        		if ( reverse.getSolution() != ""){
	        			solution = reverse.getSolution();
	        		}
	        	}
            }
//            FileOutputStream f = new FileOutputStream("out.txt");
//            for (int i=0; i < solution.length(); i++){
//                f.write((byte)solution.charAt(i));
//              }
//            f.flush();
            
//            //send the solution to the server
//            lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
//			lOut=new PrintWriter(lSocket.getOutputStream());
//			lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
//            lOut.println(pArgs[2]);
//            lOut.flush();
//            for(int i=0;i<lNumRows+1;i++)
//            {
//                lLine=lIn.readLine();
//            }
            
            lOut.println(solution);
            lOut.flush();
    
            //read answer from the server
            lLine=lIn.readLine();
    
            System.out.println("!! "+lLine);

            lOut.close();
            lSocket.close();
            
            
//            System.out.println (" SELFTEST #################");
//            Tester.runSelfTest();
            
            if ( reverse.isAlive()){
            	reverse.stop();
            }
            if ( forward.isAlive()){
            	forward.stop();
            }
            
            System.out.println("END");
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
	}

}
